package com.yhgm.service.impl;

import com.google.gson.Gson;
import com.yhgm.dao.ImageSearchDao;
import com.yhgm.entity.ImageSearch;
import com.yhgm.pojo.Image;
import com.yhgm.service.ImageSearchService;
import com.yhgm.utils.DateConverter;
import com.yhgm.utils.ESClient;
import com.yhgm.utils.QueryBuilder;
import com.yhgm.utils.R;
import io.github.classgraph.json.JSONUtils;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@Service
public class ImageSearchServiceImpl implements ImageSearchService {
    //    @Resource
//    private ImageSearchDao imageSearchDao;
    @Resource
    private ESClient esClient;

    @Override
    public R search(ImageSearch imageSearch,Integer page,Integer size) {
        SearchRequest req = new SearchRequest("image_search");
        SearchResponse rep = null;
        req= QueryBuilder.createReq(req,imageSearch);
        req.source().from(page).size(size);
        try {
            rep = esClient.getClient().search(req, RequestOptions.DEFAULT);
            SearchHits hits = rep.getHits();
            Long total = hits.getTotalHits().value;
            SearchHit[] hitsData = hits.getHits();
            List<ImageSearch> list = new ArrayList<>();
            Gson gson = new Gson();
            for (SearchHit hitData : hitsData) {
                list.add(gson.fromJson(hitData.getSourceAsString(), ImageSearch.class));
            }
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("total", total);
            data.put("list", list);
            return R.ok(data, "查询数据成功");
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("服务器查询引擎异常...");
        }
    }

    @Override
    public R insert(ImageSearch imageSearch) {
        IndexRequest req = new IndexRequest("image_search").id(imageSearch.getId());
        req.source(new Gson().toJson(imageSearch), XContentType.JSON);
        try {
            esClient.getClient().index(req, RequestOptions.DEFAULT);
        } catch (IOException e) {
            //todo:add sys-logs
            //e.printStackTrace();
            return R.ok("插入数据成功");
        }
        return R.error("插入数据成功");
    }

    @Override
    public R getById(Long id) {
        GetRequest req = new GetRequest("image_search", id.toString());
        GetResponse rep = null;
        try {
            rep = esClient.getClient().get(req, RequestOptions.DEFAULT);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("esRep", new Gson().fromJson(rep.getSourceAsString(), ImageSearch.class));
            //data.put("doc", rep.getSourceAsString());
            return R.ok(data, "查询数据成功");
        } catch (IOException e) {
            e.printStackTrace();
            return R.error("服务器查询引擎异常...");
        }
    }
}
